﻿var controller = this.lookupController();
var viewStatus = controller.analysis().status;
var vm = this.lookupViewModel();
var destInstId = controller.destInstId;
var procInstId = controller.procInstId;
var draftId = PAI.Global.getUrlData('draftId');

//页面上显示发起单号，发起时间，单前节点审批人
Ext.PAIRest.request({
    url: 'formActivity/currentProcInst',
    ns: PAI.application.userServerUrl,
    method: 'POST',
    data: {
        destInstId: destInstId || '',
        procInstId: procInstId || ''
    },
    success: function (data) {
        var empId = PAI.Global.getAccount().employee.id;
        var item, no, cache = [];
        var activity;
        if (data.currentActivitys.length == 0) {
            ref.currentApprover.hide();
        }
        for (var i = 0; data.currentActivitys.length > i; i++) {
            controller.currentActivity = data.currentActivitys[i].activityName; //节点名称
            //controller.currentActivityId = data.currentActivitys[i].activityId;
            activityName = data.currentActivitys[i].activityName;
            for (var j = 0; data.currentActivitys[i].empList.length > j; j++) {
                controller.employeeId = data.currentActivitys[i].empList[j].employeeId;
                //if(empId == controller.employeeId) {
                activity = controller.currentActivity; //节点名称
                if (activity == activityName) {
                    item = data.currentActivitys[i].empList[j];
                    no = item.employeeNo || '空';
                    if (item.defaultLanguage == 'en') {
                        cache.push(item.lastName + ' ' + item.firstName + '(' + no + ')');
                    }
                    else {
                        cache.push(item.lastName + item.firstName + '(' + no + ')');
                    }
                }
                //}
            }
        }
        ref.currentApprover.setValue(cache.join(';'));
    },
    failure: function (data) {
    }
});
//重新提交页面是否勾选判读
if (viewStatus == 'resubmit') {
    if (formData.companyResourcesRequest.companyResources.accessCard == '1' && formData.agencyInfo.jlrOffice.shanghai_New_Bund_Office == '1') {
        ref.newBundFloors.setAllowBlank(false);
        ref.newBundFloors.setDisabled(false);
    } else {
        ref.newBundFloors.setAllowBlank(true);
        ref.newBundFloors.setDisabled(true);
    }
} else if (viewStatus == 'approval') {
    var fieldNameList = ['costCenter', 'prs', 'prsAmounts', 'pos', 'posAmounts', 'email'];

    Ext.each(fieldNameList, function (field) {
        ref[field].setAllowBlank(true);
    });
}

setTimeout(function () {
    var workLocationValue = ref.workLocation.getValue();
    var hasNewBundOffice = workLocationValue.shanghai_New_Bund_Office == 1;

    var companyResourcesValue = ref.companyResources.getValue();
    var hasAccessCard = companyResourcesValue.accessCard == 1;

    ref.newBundFloors.setDisabled(hasNewBundOffice == 0 && hasAccessCard == 0);

}, 300);

if (formData.approvalInfo) {
    formData.approvalInfo.approverDesc = '';
}
ref.attachment.hide();
var activity = "";
//查询流程节点
Ext.PAIRest.request({
    url: 'formActivity/currentProcInst',
    ns: PAI.application.userServerUrl,
    method: "POST",
    async: false,
    data: {
        "destInstId": PAI.Global.getUrlData('destInstId') || '',
        "procInstId": PAI.Global.getUrlData('procInstId') || ''
    },
    success: function (data) {
        if (data.currentActivitys.length > 0) {

            var destInstId = PAI.Global.getUrlData('destInstId');
            var currentActivity = data.currentActivitys.filter(function (item) {
                return item.empList.filter(function (item2) {
                    return item2.destInstId == PAI.Global.getUrlData('destInstId');
                }).length > 0;
            });

            if (currentActivity.length) {
                activity = currentActivity[0].activityName;
            }
        }
    },
    failure: function (jsondata) {
        console.log(jsondata);
    }
});

vm.set('NebulogyForm.activity', activity);
vm.set('NebulogyForm.pushStatus', '1');
vm.set('NebulogyForm.state', 'change boarding');

if (viewStatus == 'launch') {
    Ext.PAIRest.request({
        url: 'orgEmployeeRel/listOrganizationObjByEmployeeObjId',
        method: 'POST',
        data: {
            bId: PAI.Global.getAccount().employee.id,
            objFields: [{
                exp: "$." + PAI.Global.getLanguage(),
                name: "displayName",
                type: "json"
            }],
            relFields: [{
                "name": "aId"
            }]
        },
        success: function (json) {
            if (!Ext.isEmpty(json.data)) {
                vm.set('NebulogyForm.applicantInfo.department', json.data[0].basic.displayName);
                vm.set('NebulogyForm.agencyInfo.agencyDepartment', json.data[0].basic.displayName);
            }
        }
    });
}

if (viewStatus == 'approval' || viewStatus == 'launch') {
    ref.approverCDSID.hide();
    ref.approverEmail.hide();

    if (activity.indexOf('Helpdesk') != -1) {
        ref.approverCDSID.show();
        ref.approverEmail.show();

        if (formData.companyResourcesRequest.companyResources.cdsid == 0) {
            ref.approverCDSID.setAllowBlank(true);
            ref.approverCDSID.setDisabled(true);
        }

        if (formData.companyResourcesRequest.email == 'Neither') {
            ref.approverEmail.setAllowBlank(true);
            ref.approverEmail.setDisabled(true);
        }

    } else if (activity == 'Administration Department Head' || activity.startsWith('Access card application')) {
        if (formData.companyResourcesRequest.companyResources.none_ != 1) {
            if (formData.jlrOfficeStr.includes('Shanghai New Bund Office')) {
                ref.companyResources.setReadOnly(false);
                ref.newBundFloors.setReadOnly(false);
            } else {
                ref.companyResources.setReadOnly(false);
                ref.newBundFloors.setReadOnly(true);
            }
            Ext.each(ref.companyResources.items.items, function (ctr) {
                if (ctr.inputValue == 'cdsid' || ctr.inputValue == 'skypeAccount' || ctr.inputValue == 'jlrFamily' || ctr.inputValue == 'none_') {
                    ctr.setDisabled(true);
                }
            });
        }
    } else if (activity == 'Access Card Administrator') {
        if (formData.companyResourcesRequest.companyResources.locker == 1 && formData.companyResourcesRequest.companyResources.accessCard == 1) {
            ref.locker.show();
            ref.pinCode.show()
        } else if (formData.companyResourcesRequest.companyResources.accessCard == 1) {
            ref.pinCode.show()
        } else if (formData.companyResourcesRequest.companyResources.locker == 1) {
            ref.locker.show();
        }

        //ref.locker.setAllowBlank(false);
    }
}

